package com.concurrent.coline.part1.daemon;

/**
 * 守护线程
 */
public class DaemonThread {
    private static class DemoThread extends Thread {
        @Override
        public void run() {
            try {
                String threadName = Thread.currentThread().getName();
                while(!isInterrupted()){
                    System.out.println(threadName+" is running");
                }
                System.out.println(threadName+" interrput flag is "
                        +Thread.currentThread().isInterrupted());

            } finally {
                // 守护线程中finally不一定会执行
                System.out.println("attention this part is not run in daemon thread");
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        //守护线程,与主线程（main）同生共死
        DemoThread demoThread = new DemoThread();
        // 设置为守护线程，必须要在start之前
        demoThread.setDaemon(true);
        demoThread.start();
        Thread.sleep(10);
        //demoThread.interrupt();
    }
}
